# encoding=utf-8
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding()!=default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

from bs4 import BeautifulSoup
import bs4
html_doc = """
<html>
    <head>
        <title>The Dormouse's story</title>
    </head>
    <body>
        <p class="title">
            <b>The Dormouse's story</b>
        </p>
        <p class="story">Once upon a time there were three little sisters; and their names were
            <a href="http://example.com/elsie" class="sister" id="link1"><!--Elsie--></a>,
            <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
            <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
            and they lived at the bottom of a well.
        </p>
        <p class="story">...</p>
    </body>
</html>
"""
# 四大类
# Tag
    # 有两个属性：name,attr
# NavigableString
# Beautifulsoup
# Comment

# Tag
soup = BeautifulSoup(html_doc,"html.parser")
print soup.p.get('class')   # 获得一个属性
print soup.p.attrs          # 获得全部属性
soup.p["class1"]="new"      # 添加新的属性
del soup.p['class']         # 删除一个属性
print type(soup.p)          # p的属性类
# NavigableString 获得标签内部的字符串
print soup.p.b.string
print type(soup.p.b.string)     # string的属性类
# Beautifulsoup 表示一个文档的全部内容
print type(soup.name)
print soup.name
print soup.attrs
# Comment 是一个特殊的NavigableString对象 判断string是不是注释的
print soup.a
print soup.a.string#有注释的 但是输出却是没有注释的
print type(soup.a.string)
if type(soup.a.string) == bs4.element.Comment:    # 进行判断
    print "注释为："+soup.a.string
